﻿using System.IO;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Packet.Records;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.Serializer.Stream.Extension;
using System.Net.DnsToolkit.Packet.Busy.Records;

namespace System.Net.DnsToolkit.Serializer.Stream.Reader.Record {
    public class RecordSOAReader : IRecordReader<System.IO.Stream> {
        public QTypeEnum QTypeEnum {
            get {
                return QTypeEnum.SOA;
            }
        }

        public IRecord Read(System.IO.Stream source) {

            IRecord record;
            InternalRead(false, source, out record);
            return record;

        }

        public bool TryRead(System.IO.Stream source, out IRecord record) {
            return InternalRead(true, source, out record);
        }

        private bool InternalRead(bool tryRead, System.IO.Stream source, out IRecord record) {

            var soa = new RecordSOA {
                MNAME = source.ReadDomainName(),
                RNAME = source.ReadDomainName(),
                SERIAL = source.Read32(),
                REFRESH = source.Read32(),
                RETRY = source.Read32(),
                EXPIRE = source.Read32(),
                MINIMUM = source.Read32()
            };

            record = soa;
            return true;

        }
    }
}
